#!/usr/bin/php

<?php

@error_reporting(E_ALL ^ E_NOTICE);
@set_time_limit(0);

require('./includes/dbconfig.php');
require('./includes/api/class.api.php');
require('./includes/api/class.charselect.php');
require('./includes/api/class.charactername.php');
require('./includes/api/class.industryjobs.php');

// Set default timezone to match that of EVE API Server
date_default_timezone_set('UTC');
$api = new Api();

//Connect to DB
$link = mysql_connect($dbserver, $dbuser, $dbpass)
or die('Could not connect: ' . mysql_error());
mysql_select_db($dbase) or die('Could not select database');

// Retrieve API keys and fetch extra details from API
$sql    = 'SELECT * FROM `apikeys` WHERE `director` = 1';
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$api = new Api();

// Set API full logon details for corp info access
$api->setCredentials($row['userID'], $row['apiKey'], $row['charID']);
$corp = true;
$apijobsxml = $api->getIndustryJobs($corp,30);
$apijobs    = array();
$apijobs    = IndustryJobs::getIndustryJobs($apijobsxml);

foreach($apijobs as $index => $thisjob)
{
  // Query DB for typeName for each job
  $itemsql    = 'SELECT * FROM `invtypes` WHERE `typeID` = '.$thisjob['installedItemTypeID'];
  $itemresult = mysql_query($itemsql);
  $itemrow    = mysql_fetch_array($itemresult,MYSQL_ASSOC);

  // Query DB for industrial activity
  $statussql  = 'SELECT * FROM `tl2materialsactivity` WHERE `activityID` = '.$thisjob['activityID'];
  $statusresult = mysql_query($statussql);
  $statusrow  = mysql_fetch_array($statusresult,MYSQL_ASSOC);

  // Get charName from API for installer
  $charnamexml= $api->getCharacterName($thisjob['installerID']);
  $charname   = CharacterName::getCharacterName($charnamexml);

  // Calculate time columns
  $timetilend = strtotime($thisjob['endProductionTime'])-time();
  $enddate    = date_create($thisjob['endProductionTime']);
  date_timezone_set($enddate,timezone_open('Europe/London'));
  $enddatestr = date_format($enddate, 'd/m D - H:i');
  $days       = floor($timetilend / 86400);
  $hours      = floor(($timetilend - ($days * 86400)) / 3600);
  $minutes    = floor(($timetilend - ($days * 86400) - ($hours * 3600)) / 60);
  $timeleft   = $days.'d '.$hours.'h '.$minutes.'m';

  // Add new 'columns' to array
  $thisjob['installTime']         = strtotime($thisjob['installTime']);
  $thisjob['beginProductionTime'] = strtotime($thisjob['beginProductionTime']);
  $thisjob['endProductionTime']   = strtotime($thisjob['endProductionTime']);
  $thisjob['pauseProductionTime'] = strtotime($thisjob['pauseProductionTime']);
  $thisjob['installer']           = $charname[0]['name'];
  $thisjob['typeName']            = $itemrow['typeName'];
  $thisjob['activity']            = $statusrow['activityName'];
  $thisjob['enddatestr']          = $enddatestr;
  $thisjob['timeleft']            = $timeleft;
  $thisjob['timetilend']          = $timetilend;

  // REPLACE industry jobs into DB
  $sql = 'REPLACE INTO `jobsarchive` VALUES('.$thisjob['jobID'].','.$thisjob['assemblyLineID'].','.$thisjob['containerID'].','.
    $thisjob['installedItemID'].','.$thisjob['installedItemLocationID'].','.$thisjob['installedItemQuantity'].','.$thisjob['installedItemProductivityLevel'].','.
    $thisjob['installedItemMaterialLevel'].','.$thisjob['installedItemLicensedProductionRunsRemaining'].','.$thisjob['outputLocationID'].','.
    $thisjob['installerID'].','.$thisjob['runs'].','.$thisjob['licensedProductionRuns'].','.$thisjob['installedInSolarSystemID'].','.
    $thisjob['containerLocationID'].','.$thisjob['materialMultiplier'].','.$thisjob['charMaterialMultiplier'].','.$thisjob['timeMultiplier'].','.
    $thisjob['charTimeMultiplier'].','.$thisjob['installedItemTypeID'].','.$thisjob['outputTypeID'].','.$thisjob['containerTypeID'].','.
    $thisjob['installedItemCopy'].','.$thisjob['completed'].','.$thisjob['completedSuccessfully'].','.$thisjob['installedItemFlag'].','.
    $thisjob['outputFlag'].','.$thisjob['activityID'].','.$thisjob['completedStatus'].','.$thisjob['installTime'].','.
    $thisjob['beginProductionTime'].','.$thisjob['endProductionTime'].','.$thisjob['pauseProductionTime'].',"'.$thisjob['installer'].'","'.
    $thisjob['typeName'].'","'.$thisjob['activity'].'","'.$thisjob['enddatestr'].'","'.$thisjob['timeleft'].'",'.$thisjob['timetilend'].');';
  $result = mysql_query($sql);
   /* $row    = mysql_fetch_array($itemresult,MYSQL_ASSOC);*/
}
unset($value);
?>
